﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace yyycommon
{
    public static class ProgramUtil
    {
        public static bool ProcExist(string ProcName)
        {
            string tempName = "";
            int begpos;
            int endpos;
            foreach (System.Diagnostics.Process thisProc in System.Diagnostics.Process.GetProcesses())
            {
                tempName = thisProc.ToString();
                begpos = tempName.IndexOf("(") + 1;
                endpos = tempName.IndexOf(")");
                tempName = tempName.Substring(begpos, endpos - begpos);
                if (tempName == ProcName) { 
                    return true;
                }
            }
            return false;
        }
        public static bool CloseProc(string ProcName)
        {
            bool result = false;
            System.Collections.ArrayList procList = new System.Collections.ArrayList();
            string tempName = "";
            int begpos;
            int endpos;
            foreach (System.Diagnostics.Process thisProc in System.Diagnostics.Process.GetProcesses())
            {
                tempName = thisProc.ToString();
                begpos = tempName.IndexOf("(") + 1;
                endpos = tempName.IndexOf(")");
                tempName = tempName.Substring(begpos, endpos - begpos);
                procList.Add(tempName);
                if (tempName == ProcName)
                {
                    if (!thisProc.CloseMainWindow())
                        thisProc.Kill();         //当发送关闭窗口命令无效时强行结束进程
                    result = true;
                    break;
                }
            }
            return result;
        }
    }
}
